﻿using System;
using System.Text.RegularExpressions;
using ToolGood.Words.Pinyin;

namespace ORS.OrderRobot.Service
{
	public interface NameParser
	{
        public static (string firstName,string lastName) Parse(string input)
        {
            input = input.Trim();
            bool isChinese = Regex.IsMatch(input, @"[\u4e00-\u9fa5]+");

            if (isChinese)
            {
                var pinyins = WordsHelper.GetPinyinList(input);
                if (pinyins == null || pinyins.Length < 2) throw new ArgumentException("中文姓名至少两个字");

                string firstName = string.Concat(pinyins.Skip(1).Select(Capitalize));
                string lastName = Capitalize(pinyins[0]);

                return (firstName.ToUpper(), lastName.ToUpper());
            }
            else
            {
                var parts = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (parts.Length < 2)
                    throw new ArgumentException("拼音输入需要包含姓和名");

                string firstName = string.Concat(parts.Skip(1).Select(Capitalize));
                string lastName = Capitalize(parts[0]);

                return (firstName.ToUpper(),lastName.ToUpper());
            }
        }

        private static string Capitalize(string s)
        {
            if (string.IsNullOrWhiteSpace(s)) return s;
            return s[0] + s.Substring(1);
        }
    }
}

